//
//  picnewsViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/11/15.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "picxqViewController.h"
#import "picnewsViewController.h"
#import "picnewsCollectionViewCell.h"
@interface picnewsViewController ()

@end

@implementation picnewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图片新闻";
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc]initWithFrame:self.view.frame];

UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.1];
view.backgroundColor = myColor;
[self.view addSubview:view];
     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
     layout.minimumLineSpacing = 10; //设置行间距
layout.minimumInteritemSpacing = 10; //设置列间距
  _CollectionView =  [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
  _CollectionView.backgroundColor = [UIColor clearColor];
          layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _CollectionView.dataSource = self;
        //设置代理,监听
        _CollectionView.delegate = self;
            self.CollectionView.alwaysBounceVertical = YES;
    [_CollectionView registerClass:[picnewsCollectionViewCell class] forCellWithReuseIdentifier:@"picnewsid"];
[self.view addSubview:_CollectionView];
   __weak typeof(self) weakSelf = self;

    [self.CollectionView addHeaderWithCallback:^{
            weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据

    
    }];
    [self.CollectionView headerBeginRefreshing];
    [self.CollectionView addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
       
  
     [weakSelf loadMore];
 
        }else{
            
            [weakSelf.CollectionView footerEndRefreshing];
            [weakSelf.view makeToast:@"没有更多了"];
            
        }
        
    }];




    // Do any additional setup after loading the view.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    picnewsCollectionViewCell *mCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picnewsid" forIndexPath:indexPath];

mCell.dic = _dataArray[indexPath.row];

return mCell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row ==0){
return CGSizeMake(KScreenWidth,200);

}else{
return CGSizeMake((KScreenWidth-20)/2,150);


}
}
- (void)loadData{

    NSString *url = [NSString stringWithFormat:@"%@pageNum=%d",kimage,_currentPage];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
_dataArray =   [NSMutableArray array];
  NSArray *array = dictionary[@"list"];
    for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
  self.totalCounts =[dictionary[@"totalRow"] intValue];

[_CollectionView reloadData];
        [self.CollectionView headerEndRefreshing];
        self.currentPage++;
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
         [self.CollectionView headerEndRefreshing];

 }];

}
- (void)loadMore{

    NSString *url = [NSString stringWithFormat:@"%@pageNum=%d",kimage,_currentPage];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 
 NSArray *array = dictionary[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}        self.currentPage++;

  self.totalCounts =[dictionary[@"totalRow"] intValue];
        [self.CollectionView footerEndRefreshing];
[_CollectionView reloadData];
    
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{
         [self.CollectionView footerEndRefreshing];

 }];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 NSDictionary  *dic = _dataArray[indexPath.row];
    NSLog(@"%zd",indexPath.item);
    picxqViewController *vc = [[picxqViewController alloc]init];
    vc.id = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 告诉系统每组多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
